<template>
  <el-config-provider :size="getGlobalComponentSize" :locale="getGlobalI18n" :z-index="100">
    <router-view v-show="setLockScreen" />
    <LockScreen v-if="themeConfig.isLockScreen" />
    <Settings ref="settingsRef" v-show="setLockScreen" />
    <CloseFull v-if="!themeConfig.isLockScreen" />
    <UpgradeIndex v-if="isVersion" />
  </el-config-provider>
</template>

<script setup lang="ts">
import { ref, watch, computed, onMounted, onBeforeMount, defineAsyncComponent } from 'vue'
import { useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useTagsViewRoutes } from '@/stores/tagsViewRoutes'
import { themeConfig, token } from '@/storage'
import { Local, Session } from '@/utils/storage'
import other from '@/utils/other'
import setIntroduction from '@/utils/setIconfont'

// 引入组件
const LockScreen = defineAsyncComponent(() => import('@/layout/lockScreen/lockScreen.vue'))
const Settings = defineAsyncComponent(() => import('@/layout/navBars/topBar/SystemSettings.vue'))
const CloseFull = defineAsyncComponent(() => import('@/layout/navBars/topBar/closeFull.vue'))
const UpgradeIndex = defineAsyncComponent(() => import('@/layout/upgrade/UpgradeIndex.vue'))

defineOptions({ name: 'app' })

// 定义变量内容
const { messages, locale } = useI18n()
const isVersion = ref(false)
const settingsRef = ref()
const route = useRoute()
const stores = useTagsViewRoutes()

// 设置锁屏时组件显示隐藏
const setLockScreen = computed(() => {
  // 防止锁屏后，刷新出现不相关界面
  return themeConfig.value.isLockScreen
    ? themeConfig.value.lockScreenTime > 1
    : themeConfig.value.lockScreenTime >= 0
})
// 获取版本号（返回是否有更新）
const getVersion = () => {
  if (token.value) {
    const version = Local.get('version')
    // @ts-ignore
    const current = __NEXT_VERSION__
    let bool = false
    if (!version) Local.set('version', current)
    else if (version !== current) bool = true
    isVersion.value = bool
  }
}
// 获取全局组件大小
const getGlobalComponentSize = computed(() => {
  return other.globalComponentSize()
})
// 获取全局 i18n
const getGlobalI18n = computed(() => {
  return messages.value[locale.value]
})

const onLinkFavicon = () => {
  const link = document.getElementById('favicon')
  console.log('import.meta.env.VITE_ENV', import.meta.env.VITE_ENV)
  if (link) link.setAttribute('href', `/${import.meta.env.VITE_ENV || 'favicon'}.ico`)
}

// 设置初始化，防止刷新时恢复默认
onBeforeMount(() => {
  // 未登录关闭锁屏
  if (!token.value) themeConfig.value.isLockScreen = false
  // 设置批量第三方 icon 图标
  setIntroduction.cssCdn()
  // 设置批量第三方 js
  setIntroduction.jsCdn()
})

// 页面加载时
onMounted(() => {
  onLinkFavicon()
  // 获取缓存中的布局配置
  const configStyle = Local.get('themeConfigStyle')
  if (configStyle) document.documentElement.style.cssText = configStyle
  // 获取缓存中的全屏配置
  const isTagViewCurrenFull = Session.get('isTagViewCurrenFull')
  if (isTagViewCurrenFull) stores.setCurrenFullscreen(isTagViewCurrenFull)
  getVersion()
})
// 监听路由的变化，设置网站标题
watch(
  () => route.path,
  () => {
    other.useTitle()
  },
  {
    deep: true
  }
)
</script>
<style lang="scss">
@import '@/views/system/flow/workflow/scss/index.scss';
</style>
